package book.manage.sql;

import book.manage.dao.IInputDao;
import book.manage.po.Book;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.function.Consumer;

public class SqlUtil {
    private SqlUtil(){}
    private static SqlSessionFactory factory;

    static {
        try {
            factory= new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis/mybatis-config.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void doSqlWork(Consumer<IInputDao> consumer){
        try(SqlSession session = factory.openSession(true)){
            IInputDao input = session.getMapper(IInputDao.class);
            consumer.accept(input);
        }
    }
}
